package com.socialuni.center.web.utils;

import com.socialuni.center.web.exception.SocialUserBannedException;
import com.socialuni.center.web.model.DO.oAuth.ThirdUserDO;
import com.socialuni.center.web.model.DO.oAuth.ThirdUserTokenDO;
import com.socialuni.center.web.model.DO.user.UserDetailDO;
import com.socialuni.center.web.repository.oAuth.ThirdUserRepository;
import com.socialuni.center.web.repository.user.UserDetailRepository;
import com.socialuni.sdk.constant.status.UserStatus;
import com.socialuni.sdk.exception.SocialNullUserException;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class CenterUserUtil {
    private static ThirdUserRepository thirdUserRepository;
    private static UserDetailRepository userDetailRepository;

    @Resource
    public void setThirdUserRepository(ThirdUserRepository thirdUserRepository) {
        CenterUserUtil.thirdUserRepository = thirdUserRepository;
    }

    @Resource
    public void setUserDetailRepository(UserDetailRepository userDetailRepository) {
        CenterUserUtil.userDetailRepository = userDetailRepository;
    }

    public static Integer getMineUserId() {
        UserDO user = CenterUserUtil.getMineUser();
        if (user == null) {
            return null;
        }
        return user.getId();
    }

    public static UserDO getMineUser() {
        ThirdUserTokenDO tokenDO = CenterTokenUtil.getThirdUserTokenDO();
        return getMineUser(tokenDO);
    }

    public static boolean isMine(Integer userId) {
        Integer mineUserId = CenterUserUtil.getMineUserId();
        return userId.equals(mineUserId);
    }

    public static UserDO getMineUser(String token) {
        ThirdUserTokenDO tokenDO = CenterTokenUtil.getThirdUserTokenDO(token);
        return getMineUser(tokenDO);
    }

    private static UserDO getMineUser(ThirdUserTokenDO tokenDO) {
        if (tokenDO == null) {
            return null;
        }
        UserDO user = UserUtils.get(tokenDO.getUserId());
        if (user.getStatus().equals(UserStatus.violation)) {
            throw new SocialUserBannedException(user);
        }
        //返回user
        return user;
    }


    public static ThirdUserDO getMineThirdUser() {
        Integer devId = DevAccountUtils.getDevId();
        Integer userId = CenterUserUtil.getMineUserId();
        ThirdUserDO thirdUserDO = thirdUserRepository.findByDevIdAndUserId(devId, userId);
        return thirdUserDO;
    }

    public static String getMineUserPhoneNum() {
        UserDO userDO = CenterUserUtil.getMineUser();
        return UserUtils.getUserPhoneNum(userDO.getId());
    }

    public static UserDO get(String userId) {
        Integer id = UnionIdDbUtil.getUserIdByUid(userId);
        return UserUtils.get(id);
    }

    public static UserDetailDO getUserDetail(UserDO userDO) {
        return CenterUserUtil.getUserDetail(userDO.getId());
    }

    public static UserDetailDO getUserDetail(Integer userId) {
        UserDetailDO commonUserDOOptional = userDetailRepository.findOneByUserId(userId);
        if (commonUserDOOptional == null) {
            throw new SocialNullUserException();
        }
        return commonUserDOOptional;
    }
}